<template>
  <div class="page-box">
    <!-- 工具条区域 -->
    <div class="tabbar-box">
      <el-row type="flex" justify="space-between">
        <div>
          <el-form :inline="true" class="demo-form-inline">
            <el-form-item>
              <el-input v-model="queryData.role_name" placeholder="角色名称" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" plain @click="search">搜索</el-button>
              <el-button @click="resetList">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
        <div>
          <el-button type="primary" plain @click="dialogVisible = true">创建角色</el-button>
        </div>
      </el-row>
    </div>
    <!-- 表格区域 -->
    <div class="table-box">
      <el-table :data="list" style="width: 100%" border>
        <el-table-column prop="id" label="角色ID" />
        <el-table-column prop="role_name" label="角色名称" />
        <el-table-column prop="identify" label="角色标识" />
        <el-table-column prop="description" label="角色描述" />
        <el-table-column prop="created_at" label="添加时间" />
        <el-table-column label="操作" align="center">
          <template #default="{ row }">
            <el-button type="primary" icon="el-icon-edit" circle @click="edit(row.id)" />
            <el-button type="danger" icon="el-icon-delete" circle @click="delList(row.id)" />
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页区域 -->
    <div>
      <el-row type="flex" justify="end">
        <el-pagination
          background
          :current-page="queryData.page"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="queryData.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-row>
    </div>
    <!-- 弹出层 -->
    <div>
      <el-dialog
        :title="title"
        :visible.sync="dialogVisible"
        width="600px"
        :before-close="handleClose"
        border
      >
        <el-form ref="longRef" :model="longModel" :rules="longRules">
          <el-form-item prop="role_name" label="角色名称">
            <el-input
              v-model="longModel.role_name"
              placeholder="请输入角色名称"
              style="width: 450px"
            />
          </el-form-item>
          <el-form-item prop="identify" label="角色标识">
            <el-input
              v-model="longModel.identify"
              placeholder="请输入角色标识"
              style="width: 450px"
            />
          </el-form-item>
          <el-form-item prop="description" label="角色描述">
            <el-input
              v-model="longModel.description"
              placeholder="请输入角色描述"
              type="textarea"
              style="width: 450px; height: 50px"
            />
          </el-form-item>
          <el-form-item label="角色权限" prop="authlist">
            <div style="margin-left:70px">
              <!-- :default-checked-keys:默认勾选的节点的key的数组，需要配合node-key使用 -->
              <el-tree
                ref="tree"
                :check-strictly="false"
                node-key="id"
                :default-checked-keys="permIds"
                :data="treePerList"
                :props="defaultProps"
                show-checkbox
                accordion
              />
            </div>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="handleClose">取消</el-button>
          <el-button type="primary" @click="btnOk">确定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getfromList, createList, delCharacters, editRole, CharactersId } from '@/api/management/role'
// import { transListToTreeData } from '@/utils'
import { getList } from '@/api/management/menu'

export default {
  data() {
    return {
      defaultProps: {
        children: 'children',
        label: 'permission_name'
      },
      permIds: [], // 角色拥有的权限的id的列表
      currentId: null,
      treePerList: [], // 存放权限的树形结构的数据
      dialogVisible: false, // 控制弹层的显示和隐藏
      list: [], // 整个表格的列表
      queryData: {
        id: '',
        role_name: '',
        page: 1,
        limit: 10
      },
      total: 0, // 数据总量
      // 弹出层的数据
      longModel: {
        role_name: '',
        identify: '',
        description: '',
        authlist: []
      },
      longRules: { // 校验规则
        role_name: [
          {
            required: true,
            message: '请输入角色名称',
            trigger: 'blur'
          }
        ],
        identify: [
          {
            required: true,
            message: '请输入角色标识',
            trigger: 'blur'
          }
        ],
        description: [
          {
            required: true,
            message: '请输入角色描述',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {
    title() {
      return this.longModel.id ? '编辑角色' : '添加角色'
    }
  },
  created() {
    this.initData()
    this.getTreePerList()
  },
  methods: {
    // 渲染整个列表
    async initData() {
      const res = await getfromList(this.queryData)
      console.log(res, 'res')
      this.list = res.data.list
      this.total = res.data.total
    },
    //  搜索
    search() {
      this.queryData.page = 1
      this.initData()
    },
    //  重置
    resetList() {
      this.queryData = {
        role_name: '',
        page: 1,
        limit: 10
      }
      this.initData()
    },
    // 获取树形选择框
    async getTreePerList() {
      const res = await getList()
      console.log(res)
      this.treePerList = res.data
    },
    // 弹出层关闭的回调函数
    handleClose() {
      this.dialogVisible = false // 关闭弹框
      this.longModel = { // 清空内容
        role_name: '',
        identify: '',
        description: '',
        authlist: []
      }
      this.$refs.longRef.clearValidate() // 重置校验
    },
    // 创建角色
    async btnOk() {
      await this.$refs.longRef.validate() // 表单校验
      if (this.longModel.id) {
        // console.log(this.$refs.tree.getCheckedKeys())
        this.longModel.authlist = this.$refs.tree.getCheckedKeys()
        await editRole(this.longModel) // 有id是编辑
      } else {
        // console.log(this.$refs.tree.getCheckedKeys())
        // this.$refs.tree.getCheckedKeys() 获取树节点选中的节点的id组成的数组
        this.longModel.authlist = this.$refs.tree.getCheckedKeys()
        await createList(this.longModel) // 无id是新增
      }
      // 提示消息
      this.$message.success(this.longModel.id ? '修改成功' : '添加成功')
      // 关闭弹框
      this.dialogVisible = false
      // 列表更新’
      this.initData()
    },
    // 编辑功能
    async edit(id) {
      // 打开弹框
      this.dialogVisible = true
      const res = await CharactersId(id)
      console.log(res)
      this.longModel = res.data
      this.$refs.tree.setCheckedNodes(this.longModel.authlist)
    },
    // 删除
    async delList(id) {
      this.$confirm('你确定要删除这个角色吗，是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await delCharacters(id)
          console.log(res)
          this.initData()
          this.$message({ type: 'success', message: '删除成功' })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 分页器
    handleSizeChange(val) {
      this.queryData.page = 1
      this.queryData.limit = val
      this.initData()
    },
    handleCurrentChange(val) {
      this.queryData.page = val
      this.initData()
    }

  }
}
</script>
<style lang="scss" scoped>
.page-box {
  padding: 20px;
  .tabbar-box {
    padding-top: 10px;
    // border:  1px solid #eee;
    margin-bottom: 20px;
    height: 50px;
  }
}
</style>
